<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title>台风实时监测与预测</title>

    <style type="text/css">
        <!--
        body {
            font-size: 15pt
        }

        td {
            font-size: 15pt
        }

        -->
    </style>

    <style type=text/css>
        body {
            background-image: url(back_real.jpg);
            background-repeat: no-repeat;
            background-position: center top;
        }
    </style>
</head>
<body>
<?php
/**
 * 查询实时台风数据
 *
 * @author 周利发    zhoulifa@126.com
 */
error_reporting(~E_DEPRECATED);

// ------基本设定------
$config = parse_ini_file("config/db_config.ini");
$server = $config["server"];
$username = $config["username"];
$password = $config["password"];
$database = $config["database"];
$conn = mysql_connect($server, $username, $password);
mysql_select_db($database, $conn);

// ------保存待预测实时台风编号数组------
$num_forecast = array();
$count_forecast = 0;

// ------构建实时台风编号------
$query = 'SELECT max(num) FROM `typhoon_detail` WHERE 1';
$result = mysql_query($query);
while ($num_result = mysql_fetch_row($result)) {
    // ------输出表的记录------
    // for($i = 0; $i < mysql_num_fields ( $result ); $i ++) {
    // echo $num_result [$i] . ' ';
    // }
    // echo '<br>';
    $num_dao = $num_result [0];
}
$num_dao = date('Y') . substr($num_dao, 6, 2)-2;
// echo $num_dao . '<br />';
$num_dao=201522;

for ($j = 0; $j < 99; $j++) {
    $num_dao = $num_dao + $j;
    // ------打开$htp网址，读取html代码------
    $htp = 'http://www.ptsl.gov.cn/WEBSYQ/rewin.asp?id=' . $num_dao;
    $lines = file($htp);
    // ------用于匹配html代码中的字符------
    $str_match = 'parent.drawPoint1';
    $str_match_len = 17;
    // ------正则表达式，用于匹配台风记录点信息------
    $pattern = '#&quot;(.*?)&quot;#i';
    $pattern_lalo = '#&quot;,(.*?),&quot;#i';

    foreach ($lines as $line_num => $line) {
        // ------将HTML代码转化为普通字符------
        $line_html = htmlspecialchars($line);
        // echo $line_html.'<p>';
        // ------截取从str_match开始的HTML代码------
        if (stristr($line_html, $str_match)) {
            // echo $line_html . "<br />\n";
            break;
        }
    }
    // echo $line_html . '<p>';

    $pos = strpos($line_html, $str_match);
    $line_leave = substr($line_html, $pos + $str_match_len);

    $i = 0;
    $record_str = array();
    $record_typhoon = array();
    $lat_typhoon = array();
    $long_typhoon = array();

    // ------从html代码中截取台风记录点信息，并保存于$record_str 数组中------
    while (true) {
        // ------查找台风记录点信息的位置------
        $pos = strpos($line_leave, $str_match);
        // ------在i＝0的情况下，判断是否能查找到台风记录点信息------
        if ($i == 0 && $pos == false) {
            forecast_list();
            exit ();
        } else {
            // ------若不能查找到，则表示已经循环到最后一个记录点信息了------
            if (!$pos) {
                $pos = strpos($line_leave, 'parent.drawMap1');
                // ------截取含有记录点信息的字符串，并赋值给$record_str------
                $record_str [$i] = substr($line_leave, 0, $pos);
                // echo $record_str[$i] . '<br />';

                // ------用正则表达式匹配出$record_str 中的‘双引号’内的字符------
                preg_match_all($pattern, $record_str [$i], $record_typhoon [$i]);
                // ------用正则表达式匹配出$record_str 中的经纬度的字符串------
                preg_match_all($pattern_lalo, $record_str [$i], $record_lalo);
                // echo $record_lalo [1] [1] . '<br />';

                // ------取出经纬度信息，并存入变量数组中------
                $pos_lalo = strpos($record_lalo [1] [1], ',');
                $long_typhoon [$i] = substr($record_lalo [1] [1], 0, $pos_lalo);
                $lat_typhoon [$i] = substr($record_lalo [1] [1], $pos_lalo + 1);
                // echo $lat_typhoon [$i] . ' ' . $long_typhoon [$i] . '<br />';
                break;
            }
            // ------截取含有记录点信息的字符串，并赋值给$record_str------
            $record_str [$i] = substr($line_leave, 0, $pos);
            // echo $record_str[$i]. '<br />';
            // ------用正则表达式匹配出$record_str 中的‘双引号’内的字符------
            preg_match_all($pattern, $record_str [$i], $record_typhoon [$i]);
            // ------用正则表达式匹配出$record_str 中的经纬度的字符串------
            preg_match_all($pattern_lalo, $record_str [$i], $record_lalo);
            // echo $record_lalo [1] [1] . '<br />';

            // ------取出经纬度信息，并存入变量数组中------
            $pos_lalo = strpos($record_lalo [1] [1], ',');
            $long_typhoon [$i] = substr($record_lalo [1] [1], 0, $pos_lalo);
            $lat_typhoon [$i] = substr($record_lalo [1] [1], $pos_lalo + 1);
            // echo $lat_typhoon [$i] . ' ' . $long_typhoon [$i] . '<br />';

            // ------保存剩于的HTML代码------
            $line_leave = substr($line_leave, $pos + $str_match_len);
            $i++;
        }
    }
    // ------台风记录点变量定义------
    $count_typhoon = $i + 1;
    $name_china = array();
    $pres_typhoon = array();
    $wnd_typhoon = array();
    $no_typhoon = array();
    $date_typhoon = array();
    $hour_typhoon = array();
    $level_typhoon = array();
    $speed_typhoon = array();
    // ---记录存入数据库中的记录点数、上一个存储入数据库记录点的位置---
    $count = 0;
    $last_pos = 0;
    // ------输出台风记录------
    for ($i = 0; $i < $count_typhoon; $i++) {
        // for($j = 0; $j < 14; $j ++) {
        // echo $record_typhoon [$i] [1] [$j] . '+';
        // }
        $name_china [$i] = $record_typhoon [$i] [1] [1];
        $pres_typhoon [$i] = $record_typhoon [$i] [1] [4];
        $wnd_typhoon [$i] = $record_typhoon [$i] [1] [5];
        $num_typhoon = $record_typhoon [$i] [1] [10];
        // ------将台风编号由国际编制改为国内编制------
        $num_typhoon = substr($num_typhoon, 0, 4) . '00' . substr($num_typhoon, 4, 2);

        if ($i == 0) {
            // ------判断该台风是否为当前正在西北太平洋上发生------
            // ------查找当前日期是否有台风发生------
            $date_today = date('Y-n-j');
            // echo $date_today;
            $pos_today = strpos($line_html, $date_today);
            // echo ' ' . $pos_today;

            // ------判断当前时间是否有台风发生,若有则保存台风编号------
            if ($pos_today) {
                $num_forecast [$count_forecast] = $num_typhoon;
                $count_forecast++;
            }
        }
        $no_typhoon [$i] = $record_typhoon [$i] [1] [11];
        $date_typhoon [$i] = $record_typhoon [$i] [1] [12];
        // ------将时间拆分为日期与小时------
        $pos_date = strpos($date_typhoon [$i], ' ');
        $hour_typhoon [$i] = substr($date_typhoon [$i], $pos_date + 1);
        $date_typhoon [$i] = substr($date_typhoon [$i], 0, $pos_date);

        $pos_hour = strpos($hour_typhoon [$i], ':');
        $hour_typhoon [$i] = substr($hour_typhoon [$i], 0, $pos_hour);
        // ------将时间间隔为六个小时的台风记录点读取出来------
        if ($i != 0) {
            if ((($hour_typhoon [$last_pos] + 6) % 24) != $hour_typhoon [$i]) {
                continue;
            }
        }
        $level_temp = $record_typhoon [$i] [1] [13];
        /*
         * 0- 弱于热带低压(TD), 或等级未知,
         * 1- 热带低压(TD, 10.8-17.1m/s) 6,7级,
         * 2- 热带风暴(TS,17.2-24.4 m/s) 8,9级,
         * 3- 强热带风暴(STS, 24.5-32.6 m/s) 10,11级,
         * 4- 台风(TY, 32.7-41.4 m/s) 12,13级,
         * 5- 强台风(STY, 41.5-50.9 m/s) 14,15级,
         * 6- 超强台风(SuperTY, ≥51.0 m/s) 16级以上,
         */
        // ------将台风风速等级转化为台风强度种类------
        $pattern_level = '#(\d+)#';
        preg_match_all($pattern_level, $level_temp, $matches_level, PREG_SET_ORDER);
        $level_typhoon [$i] = $matches_level [0] [0];
        switch ($level_typhoon [$i]) {
            case 0 :
            case 1 :
            case 2 :
            case 3 :
            case 4 :
            case 5 :
                $level_typhoon [$i] = 0;
                break;
            case 6 :
            case 7 :
                $level_typhoon [$i] = 1;
                break;
            case 8 :
            case 9 :
                $level_typhoon [$i] = 2;
                break;
            case 10 :
            case 11 :
                $level_typhoon [$i] = 3;
                break;
            case 12 :
            case 13 :
                $level_typhoon [$i] = 4;
                break;
            case 14 :
            case 15 :
                $level_typhoon [$i] = 5;
                break;
            default :
                $level_typhoon [$i] = 6;
                break;
        }

        // ------计算台风移动速度------
        if ($i == 0) {
            $d = 0;
        } else {
            if ($lat_typhoon [$i] < $lat_typhoon [$i - 1]) {
                $long_c = ( float )(6371.004 * cos($lat_typhoon [$i] * pi() * 2 / 360) * 2 * pi());
            } else {
                $long_c = ( float )(6371.004 * cos($lat_typhoon [$i - 1] * pi() * 2 / 360) * 2 * pi());
            }
            // ------经度距------
            $x_2 = pow($long_c * ($long_typhoon [$i] - $long_typhoon [$i - 1]) / 360, 2);
            // ------纬度距------
            $y_2 = pow(6371.004 * 2 * pi() * ($lat_typhoon [$i] - $lat_typhoon [$i - 1]) / 360, 2);
            // echo $x_2 . "####" . $y_2 . '<br>';
            $d = sqrt($x_2 + $y_2);
        }
        // 上一个记录点到本记录点的速度(km/h)
        $speed_typhoon [$i] = $d / 6;

        // ------输出台风记录点信息------
        // echo $name_china [$i] . ' ';
        // echo $lat_typhoon [$i] . ' ';
        // echo $long_typhoon [$i] . ' ';
        // echo $pres_typhoon [$i] . ' ';
        // echo $wnd_typhoon [$i] . ' ';
        // echo $num_typhoon . ' ';
        // echo $no_typhoon [$i] . ' ';
        // echo $date_typhoon [$i] . ' ';
        // echo $level_typhoon [$i] . ' ';
        // echo $speed_typhoon [$i];
        // echo '<br />';

        if ($i == 0) {
            // -----－删除typhoon_detail数据库表中已有的待预测数据------
            $query_delete_detail = "delete from typhoon_detail where num = " . $num_typhoon;
            // echo '<br />' . $query_delete_detail . '<br />';
            $result_delete = mysql_query($query_delete_detail);
        }

        // ------将台风记录点插入typhoon_detail表中------
        $query_insert = "INSERT INTO typhoon_detail ";
        $query_insert = $query_insert . "(`num`, `date`, `hour`, `level`, `lat`, `lon`, `pres`, `wnd`, `flat`, `No`, `speed_last`) ";
        $query_insert = $query_insert . "VALUES ('";
        $query_insert = $query_insert . $num_typhoon . "', '";
        $query_insert = $query_insert . $date_typhoon [$i] . "', '";
        $query_insert = $query_insert . $hour_typhoon [$i] . "', '";
        $query_insert = $query_insert . $level_typhoon [$i] . "', '";
        $query_insert = $query_insert . $lat_typhoon [$i] . "', '";
        $query_insert = $query_insert . $long_typhoon [$i] . "', '";
        $query_insert = $query_insert . $pres_typhoon [$i] . "', '";
        $query_insert = $query_insert . $wnd_typhoon [$i] . "', '";
        $query_insert = $query_insert . '0' . "', '";
        $query_insert = $query_insert . ($count + 1) . "', '";
        $query_insert = $query_insert . $speed_typhoon [$i] . "');";
        // echo '<br />' . $query_insert . '<br />';
        $result_insert = mysql_query($query_insert);
        // if (! $result_insert && $i == 0) {
        // echo '<br />数据库导入成功';
        // }
        $count++;
        $last_pos = $i;
    }

    // ------取出$date_typhoon中的年、月、日------
    $date_temp = $date_typhoon [0];
    $str_center = '-';
    // ------取出年份------
    $pos_center = strpos($date_temp, $str_center);
    $year = substr($date_temp, 0, $pos_center);
    $date_temp = substr($date_temp, $pos_center + 1);

    // ------取出月份------
    $pos_center = strpos($date_temp, $str_center);
    $month = substr($date_temp, 0, $pos_center);
    // ------判断月份是否小于10，若小于则在其数字之前加上‘0’；例：09------
    if ($month < 10) {
        $month = '0' . $month;
    }

    // ------取出日期------
    $day = substr($date_temp, $pos_center + 1);
    // ------判断日期是否小于10，若小于则在其数字之前加上‘0’；例：09------
    if ($day < 10) {
        $day = '0' . $day;
    }

    // ------构建台风起始时间------
    if ($hour_typhoon [0] < 10) {
        $hour_typhoon [0] = '0' . $hour_typhoon [0];
    }
    $date_total = $year . $month . $day;
    $date_total = $date_total . $hour_typhoon [0];
    // echo '<br />' . $date_total . '<br />';

    // ------将中文名字改成英文------
    $query_name = "SELECT name_English FROM typhoon_name WHERE name_Chinese like '" . $name_china [0] . "'";
    // echo $query_name . '<br />';
    mysql_query("set names GBK;");
    $result_name = mysql_query($query_name);
    // ------判断是否数据库是否有数据返回------
    if (mysql_fetch_row($result_name) == 0) {
        echo '<br />此台风没有中文名字<br />';
    } else {
        mysql_data_seek($result_name, 0);
        while ($num_result = mysql_fetch_row($result_name)) {
            // ------输出表的记录------
            for ($i = 0; $i < mysql_num_fields($result_name); $i++) {
                // echo $num_result [$i] . ' ';
                $name_English = $num_result [$i];
            }
            echo '<br>';
        }
    }

    // -----－删除typhoon_total数据库表中已有的待预测数据------
    $query_delete_total = "delete from typhoon_total where num = " . $num_typhoon;
    // echo '<br />' . $query_delete_total . '<br />';
    $result_delete = mysql_query($query_delete_total);

    // ------将台风总信息插入到typhoon_total表中------
    $query_insert = "INSERT INTO typhoon_total ";
    $query_insert = $query_insert . "(`num`, `flat`, `name`, `date`, `count`, `level`) ";
    $query_insert = $query_insert . "VALUES ('";
    $query_insert = $query_insert . $num_typhoon . "', '";
    $query_insert = $query_insert . '0' . "', '";
    $query_insert = $query_insert . $name_English . "', '";
    $query_insert = $query_insert . $date_total . "', '";
    $query_insert = $query_insert . $count . "', '";
    $query_insert = $query_insert . '0' . "');";
    // echo $query_insert;
    $result_insert = mysql_query($query_insert);
}
/**
 *
 * @name forecast_list
 * @return null
 */
function forecast_list()
{
    // ------基本设定------
    $server = SAE_MYSQL_HOST_M . ':' . SAE_MYSQL_PORT;
    $username = SAE_MYSQL_USER;
    $password = SAE_MYSQL_PASS;
    $database = SAE_MYSQL_DB;
    $baseurl_fore = "http://1.bjxytyphoon.sinaapp.com//forecast.php";
    $baseurl_show = "http://1.bjxytyphoon.sinaapp.com//show.php";
    $conn = mysql_connect($server, $username, $password);
    mysql_select_db($database, $conn);

    $count_num_forecast=0;
    $num_forecast = array();
    $query_num = "SELECT distinct num FROM typhoon_detail where date =' " . date('Ymd') . "';";
    $result_num=mysql_query($query_num);
    mysql_data_seek($result_num, 0);
    while ($num_result = mysql_fetch_row($result_num)) {
        // ------输出表的记录------
//         for($i = 0; $i < mysql_num_fields ( $result_num ); $i ++) {
//         echo $num_result [$i] . ' ';
//         }
//         echo '<br>';
        $num_forecast[$count_num_forecast++] = $num_result [0];
    }

    if (!count($num_forecast)) {
        echo '<table align="center">';
        echo '<tr><td align="center"><img src="/noty.png"><td></tr>';
        echo '</table>';
    } else {
        echo '<table background="back_real_table.png" border="1" cellspacing="0px" cellpadding="2px" align="center">';
        echo '<tr><td>★★★★★★★★★★★★★★★<br/></td></tr>';
        echo '<tr><td>☆☆>>西北太平洋实时台风有：<br /></tr></td>';
        for ($i = 0; $i < count($num_forecast); $i++) {
            echo '<tr><td>&nbsp;&nbsp;&nbsp;>>序号 : ' . $num_forecast [$i] . '</tr></td>';

            // ------从数据库中将该台风编号的所有记录点取出------
            // ------构成SQL语句------
            $query = "select * from typhoon_total where num = '" . $num_forecast [$i] . "' and flat = 0;";
            $result = mysql_query($query);

            // ------判断数据库是否有返回数据------
            if (mysql_fetch_row($result) == 0) {
                echo '<br/>&nbsp;&nbsp;&nbsp;>>数据库中无法查找到数据！<br>';
                exit ();
            }
            mysql_data_seek($result, 0);
            while ($num_result = mysql_fetch_row($result)) {
                // ------输出表的记录------
                // for($i = 0; $i < mysql_num_fields ( $result ); $i ++) {
                // echo $num_result [$i] . ' ';
                // }
                // echo '<br>';
                $name_forecast = $num_result [2];
                $date_forecast = $num_result [3];
                $begin_point = $num_result [4];
            }
            // ------获取年月日------
            $date_forecast = substr($date_forecast, 0, 8);
            $year = substr($date_forecast, 0, 4);
            $month = substr($date_forecast, 4, 2);
            $day = substr($date_forecast, 6, 2);
            $date = $year . '年' . $month . '月' . $day . '日';

            // ------查找中文名字------
            $query_name = "SELECT name_Chinese FROM typhoon_name WHERE name_English like '" . $name_forecast . "'";
            // echo $query_name . '<br />';
            mysql_query("set names utf8;");
            $result_name = mysql_query($query_name);
            // ------判断是否数据库是否有数据返回------
            if (mysql_fetch_row($result_name) == 0) {
                $name_Chinese = '——';
            } else {
                mysql_data_seek($result_name, 0);
                while ($num_result = mysql_fetch_row($result_name)) {
                    // ------输出表的记录------
                    for ($j = 0; $j < mysql_num_fields($result_name); $j++) {
                        // echo $num_result [$i] . ' ';
                        $name_Chinese = $num_result [$j];
                    }
                    echo '<br>';
                }
            }

            echo '<tr><td>&nbsp;&nbsp;&nbsp;>>名称 : <font color="red">' . $name_Chinese . '</font></tr></td>';
            echo '<tr><td>&nbsp;&nbsp;&nbsp;>>时间 : ' . $date . '</tr></td>';
            echo '<tr><td>&nbsp;&nbsp;&nbsp;>>记录 : ' . $begin_point . '</tr></td>';
            echo '<tr><td>★★★★★★★★★★★★★★★<br/></tr></td>';

            // ------用表单GET发送数据给预测页面------
            echo '<tr><td align="center">';
            echo '<br />&nbsp;>>是否对此台风进行预测? <p>';
            echo '<input type="image" src="no.png" onclick="window.open(\'';
            echo $baseurl_show . '?num=' . $num_forecast[$i] . '&table=typhoon_detail\')">';
            echo '&nbsp;&nbsp;&nbsp;';
            echo '<input type="image" src="yes.png" onclick="window.open(\'';
            echo $baseurl_fore . '?num=' . $num_forecast[$i] . '&beg=' . $begin_point . '\')">';
            echo '</tr></td>';
            echo '<tr><td>★★★★★★★★★★★★★★★<br/></tr></td>';
        }
        echo '</table>';
    }
}

mysql_close($conn);
?>
</body>
</html>